package cn.tedu.staff_user.management.dao.cacge.impl;

import cn.hutool.core.bean.BeanUtil;

import cn.tedu.common.pojo.po.UserStatePO;

import cn.tedu.staff_user.management.dao.cacge.IUserCacheRepository;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.Map;

import static cn.tedu.common.consts.cache.PassportCacheConsts.KEY_PREFIX_USER_STATE;

@Repository
public class UserCacheRepositoryImpl implements IUserCacheRepository {

   @Autowired
   RedisTemplate<String,Serializable> redisTemplate;
    @Override
    public void saveUserState(Long id, UserStatePO userStatePO) {
        String key =  KEY_PREFIX_USER_STATE + id;
        Map<String,Object> map = BeanUtil.beanToMap(userStatePO);
        HashOperations<String, Object, Object> stringObjectObjectHashOperations = redisTemplate.opsForHash();


        stringObjectObjectHashOperations.putAll(key,map);
    }

    @Override
    public UserStatePO getUserState(Long id) {
        String key =  KEY_PREFIX_USER_STATE + id;
        HashOperations<String, Object, Object> stringObjectObjectHashOperations = redisTemplate.opsForHash();
        Map<Object,Object> entries = stringObjectObjectHashOperations.entries(key);
        UserStatePO userStatePO = null;
        if (entries.size()>0){
            userStatePO = BeanUtil.mapToBean(entries, UserStatePO.class, true, null);
        }


        return userStatePO;
    }
}
